package org.crazyit.activiti_d2._6_DefaultCache;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.persistence.deploy.DeploymentCache;
import org.activiti.engine.impl.persistence.deploy.ProcessDefinitionCacheEntry;

import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.Map;

public class UserLimitCache {

    public static void main(String[] args) {

        //1. 创建流程引擎
        ProcessEngineConfigurationImpl config = (ProcessEngineConfigurationImpl) ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("use-limit.cfg.xml");
        ProcessEngine engine = config.buildProcessEngine();

        //2. 得到流程存储服务实例
        RepositoryService repositoryService = engine.getRepositoryService();

        //3. 进行10次部署
        for (int i = 0; i < 10; i++) {
            repositoryService.createDeployment().addClasspathResource("default-cache.bpmn").name("dep" + i).key("key_" + i).deploy();
        }

        //4. 获取缓存
        DeploymentCache<ProcessDefinitionCacheEntry> processDefinitionCache = config.getProcessDefinitionCache();

        //5. 遍历缓存, 输出Map 中的Key
        try {
            Field cacheField = processDefinitionCache.getClass().getDeclaredField("cache");
            cacheField.setAccessible(true);
            Map map= (Map) cacheField.get(processDefinitionCache);
            for (Iterator iterator = map.keySet().iterator();iterator.hasNext();){
                String next = (String) iterator.next();
                System.out.println(next);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
